﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace arduinotest
{
    public partial class Form1 : Form
    {
        string wiadomosc;

        public Form1()
        {
            InitializeComponent();
            buttonStop.Enabled = false;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (!serialPort1.IsOpen)
            {
                serialPort1.PortName = "COM3";
                serialPort1.BaudRate = 9600;
                serialPort1.Open();
                
                buttonStop.Enabled = true;
                buttonStart.Enabled = false;

            }
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            buttonStop.Enabled = false;
            buttonStart.Enabled = true;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e) // instrukcje jakie mają się wykonac w przypadku wyłączenia okna programu
        {
            serialPort1.Close();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            wiadomosc = serialPort1.ReadLine();
            if (wiadomosc.Contains("jest sto") || wiadomosc.Contains("jest dwiescie") || wiadomosc.Contains("jest 300"))
            {
                this.Invoke(new EventHandler(DisplayText2));
            }
            this.Invoke(new EventHandler(DisplayText));
        }

        private void DisplayText(object sender, EventArgs e) // metoda wyświetlania wiadomosci
        {
            labelWiadomosc.Text = wiadomosc;
        }

        private void DisplayText2(object sender, EventArgs e) // metoda wyświetlania wiadomosci specjalnej
        {
            labeldrugi.Text = wiadomosc;
        }

        private void buttonPytanie_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                labelWiadomosc.Text = "czekam";
                
                serialPort1.WriteLine("test");

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                labelWiadomosc.Text = "czekam";
                serialPort1.WriteLine("ot");

            }
        }


    }
}
